home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12430 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  86 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: beginner question
  5. Date: Sat, 30 Mar 96 21:13:54 GMT
  6. Organization: none
  7. Message-ID: <828220434snz@genesis.demon.co.uk>
  8. References: <4jc3sr$1ggu@uvaix3e1.comp.UVic.CA> <4jdo7l$de2@sparcserver.lrz-muenchen.de> <315AFED2.7466@willows.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <315AFED2.7466@willows.com>
  15.            tarang@willows.com "Tarang Deshpande" writes:
  16.  
  17. >Kurt Watzka wrote:
  18. >> 
  19. >>   struct FOO
  20. >>   {
  21. >>     int bar;
  22. >>   }
  23. >> 
  24. >> FOO is the "struct tag" of that struct, i.e. you can use it to declare
  25. >> variables of that type as in
  26. >> 
  27. >>   struct FOO s;
  28. >> 
  29. >
  30. >So then what does the following mean:
  31. >
  32. >struct _FOO
  33.  
  34. I assume you mean 'typedef struct _FOO' here otherwise what you write
  35. below is illegal.
  36.  
  37. >{
  38. >        int     bar;
  39. >} FOO;
  40. >
  41. >struct _FOO s1;
  42. >FOO         s2;
  43.  
  44. struct _FOO is a type and is distinct from struct _BAR or struct _BAZ. typedef
  45. is a method for defining aliases for a type. So FOO above is simply an alias
  46. for struct _FOO. So given the definitions above
  47.  
  48.     FOO  x;
  49.  
  50. is just another way of writing:
  51.  
  52.     struct _FOO x;
  53.  
  54. just as if you write:
  55.  
  56.     typedef int mytype;
  57.  
  58. then:
  59.  
  60.     mytype y;
  61.  
  62. is just another way of writing:
  63.  
  64.     int y;
  65.  
  66. Structure tags are important if you want to create a self-referential
  67. or mutually referential structures e.g.
  68.  
  69.     struct elem {
  70.         struct elem *next;
  71.         int     value;
  72.     };
  73.  
  74. You couldn't do this with a typedef because the typedef name wouldn't be
  75. in scope until after the definition i.e. you can't use it within it.
  76.  
  77. Incidentally avoid identifiers that start with _ ; they are generally
  78. reserved by the language and invoke the undefined behaviour gods when you
  79. use them.
  80.  
  81. -- 
  82. -----------------------------------------
  83. Lawrence Kirby | fred@genesis.demon.co.uk
  84. Wilts, England | 70734.126@compuserve.com
  85. -----------------------------------------
  86.